package pbc.gx.ekp.addressbook.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import org.springframework.web.bind.annotation.*;
import pbc.gx.ekp.addressbook.aop.TakeTime;
import pbc.gx.ekp.addressbook.service.DeptService;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
public class DeptController {
    private final DeptService deptService;

    public DeptController(DeptService deptService) {
        this.deptService = deptService;
    }

    @TakeTime
    @RequestMapping(value = "/getDeptRoot", method = RequestMethod.GET)
    public List<Map<String, Object>> getDeptRoot() {
        return deptService.getDeptRoot();
    }

    @TakeTime
    @RequestMapping(value = "/getDepts", method = RequestMethod.GET)
    public Map<String, Object> getDepts(@RequestParam(value = "parentId") String parentId,
                                        @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        return deptService.getDepts(parentId, pageSize);
    }

    @TakeTime
    @RequestMapping(value = "/getPersons", method = RequestMethod.GET)
    public Map<String, Object> getPersons(@RequestParam(value = "departmentId", required = false) String departmentId,
                                          @RequestParam(value = "department", required = false) String department,
                                          @RequestParam(value = "name", required = false) String name,
                                          @RequestParam(value = "phone", required = false) String phone,
                                          @RequestParam(value = "pageNum") Integer pageNum,
                                          @RequestParam(value = "pageSize") Integer pageSize) {
        return deptService.getPersons(departmentId, department, name, phone, pageNum, pageSize);
    }


    @TakeTime
    @RequestMapping(value = "/downloadPersons", method = RequestMethod.GET)
    public void downloadPersons(@RequestParam(value = "departmentId", required = false) String departmentId,
                                @RequestParam(value = "department", required = false) String department,
                                @RequestParam(value = "name", required = false) String name,
                                @RequestParam(value = "phone", required = false) String phone,
                                HttpServletResponse response) throws IOException {
        String fileName = "通讯录";
        response.setContentType("application/ms-excel;charset=UTF-8");
        response.setHeader("Content-Disposition", String.format("attachment;filename=%s", URLEncoder.encode(fileName, "UTF-8") + ".xlsx"));
        List persons = deptService.getDownloadPersons(departmentId, department, name, phone);
        OutputStream outputStream = response.getOutputStream();
        List<List<String>> head = new ArrayList<>();
        List<String> head0 = new ArrayList<>();
        head0.add("序号");
        head.add(head0);
        List<String> head1 = new ArrayList<>();
        head1.add("姓名");
        head.add(head1);
        List<String> head2 = new ArrayList<>();
        head2.add("部门");
        head.add(head2);
        List<String> head3 = new ArrayList<>();
        head3.add("内线电话");
        head.add(head3);
        List<String> head4 = new ArrayList<>();
        head4.add("外线电话");
        head.add(head4);
        List<String> head5 = new ArrayList<>();
        head5.add("手机");
        head.add(head5);
        List<String> head6 = new ArrayList<>();
        head6.add("邮箱");
        head.add(head6);
        ExcelWriter excelWrite = EasyExcel.write(outputStream).head(head).build();
        WriteSheet writeSheet = EasyExcel.writerSheet("通讯录").registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()).build();
        excelWrite.write(persons, writeSheet);
        excelWrite.finish();
        excelWrite.close();
        outputStream.close();
    }

    @TakeTime
    @RequestMapping(value = "/updateDeptOrder", method = RequestMethod.GET)
    public String updateDeptOrder() {
        deptService.updateDeptOrder();
        return "成功更新部门排序";
    }

}
